<?php
// {{{ includes
load_class('Controller_BoBase');
load_class('GD_FCKeditor');
load_file('FLEA_Helper_Array');
// }}}

/**
 * 定义Controller_MBoUser
 *
 * @author jack
 * @todo 用户后台管理 
 */


class Controller_MBoUser extends Controller_BoBase {
	
	var $_modelUser;
	var $_ui;
	
	function Controller_MBoUser(){
		parent::Controller_BoBase();
		$this->_modelUser=get_singleton('Model_User');
		$this->_ui=FLEA::initWebControls();
		
	}
	
	
	function actionIndex(){
		
		$conditions='';
		$page=(int)$_GET['page'];
		$pageSize=10;
		$sort='user_id desc';
		$this->_modelUser->enableLink('department');
		$this->_modelUser->enableLink('level');
		$list = $this->_modelUser->getList($conditions,$page,$pageSize,$sort);
		 
		include(BACKEND_TPL_DIR.'/user/index.tpl.php');
	}
	
	
	function actionADD(){
		$this->_User();
	}
	
	function actionEdit(){
		$user_id=(int)$_GET['user_id'];
		if(!$user_id) redirect($this->_controllerName);
		
		$rowset=$this->_modelUser->find($user_id);
		$this->_User($rowset);
		
	}
	function actionSave(){
		//检查令牌，防止站外提交
		if(!$this->checkToken($_POST['token'])){
			$error['token']='Illegal submitted data!';
		}
		
		if($error){
			$this->_User($_POST,$error);
		}
		else{
			if($_POST['password']) $_POST['password']=md5($_POST['password']);
			else unset($_POST['password']);
			
			if($_POST['user_id']){
				$return=$this->_modelUser->update($_POST);
				$user_id=$_POST['user_id'];
				$message='User Information Updated.';
			}
			else{
				$return=$this->_modelUser->create($_POST);
				$user_id=$return;
				$message='New User Created.';
			}
			
			//--上传图片
			$rowset=array('user_id' => $user_id);

			if($_FILES['photo']['tmp_name']){				
				$photo=$this->_uploadPic('photo', 'Staff', 0, 0,$user_id);
				$rowset['photo']=$photo[1];
				$this->_modelUser->update($rowset);
			}
			
						
			if($return){
				
				$url=url($this->_controllerName,'');
				goto($url,3,$message);
			}
			else {
				$message='System error, please try again later.';
				$url=url($this->_controllerName,'');
				goto($url,3,$message);
			}
			
		}
		
		 
	}
	
	function _User($rowset='',$error=''){
		
		$action=url($this->_controllerName,'Save');
		$token=$this->getToken();
		
		//--departments
		load_class('Control_Department');
		$_ctlDetpartment = new Control_Department('department',$rowset['department']);
		$department=$_ctlDetpartment->render();
		
		//--office
		load_class('Control_Office');
		$_ctlOffice = new Control_Office('office',$rowset['office']);
		$office=$_ctlOffice->render();
		
		//--level of access
		load_class('Control_Level');
		$_ctlLevel = new Control_Level('level',$rowset['level']);
		$level=$_ctlLevel->render();
		
		include(BACKEND_TPL_DIR.'/user/user.tpl.php');
	}
	
	function actionRemove(){
		$user_id=(int)$_GET['user_id'];
		
		if($user_id) {
			$this->_Remove($user_id);
			$message = 'The user  has been removed.';
			$url=url($this->_controllerName);
			goto($url,3,$message);
		}
		else redirect(url($this->_controllerName));
	}
	
	function actionRemoveSelected(){
		if($_POST['user_id']){
			foreach ($_POST['user_id'] as $id){
				$this->_Remove($id);
			}
		}
		
		$url=url($this->_controllerName,'');
		$message='The users have been deleted.';
		goto($url,3,$message);
	}
	
	function _Remove($id){
		
		$user=$this->_modelUser->find($id);
		
		if($user){
			$file=APP_DIR.DS.get_app_inf('UserFiles').DS.$user['photo'];
			@unlink($file);
			
			return $this->_modelUser->removeByPkv($id);
			
			
		}
		else {
			return false;
		}
		
		
	}
	 
}



